<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Approval;
use App\Services\ApprovalService;
use App\Services\CompanyService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ApprovalController extends Controller
{
    protected $ApprovalService;
    public function __construct(ApprovalService $ApprovalService)
    {
        $this->ApprovalService = $ApprovalService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $model = $request->model;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $page = $page * $pageSize - $pageSize;

        $sql = $this->ApprovalService->get($model);
        $sql->where('company_id',$request->company_id);
        $sql->where('project_id',$request->project_id);

        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function wait(Request $request)
    {
        $model = $request->model;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $page = $page * $pageSize - $pageSize;

        $admin = Auth::user();
        $sql = $this->ApprovalService->wait($model,$admin->id);
        $sql->where('company_id',$request->company_id);
        $sql->where('project_id',$request->project_id);
        if($request->date){
            $date = Date('Y-m-d',$request->date / 1000);
            $sql->whereDate('created_at',$date);
        }

        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        $models = Approval::MODELS;
        foreach ($data as $k => $v) {
            $v->created_name = '';
            $v->model_name = '';
            if($v->created_user){
                $v->created_name = $v->created_user->name;
            }
            foreach ($models as $value) {
                if($v->model == $value['value']){
                    $v->model_name = $value['label'];
                }
            }
        }

        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data,'models'=>$models]);
    }

    public function launch(Request $request)
    {
        if(!$request->model){
            return response()->json(['code' => 403, 'message' => '请选择类型']);
        }
        if(!$request->source){
            return response()->json(['code' => 403, 'message' => '请选择来源']);
        }
        if(!$request->source_id){
            return response()->json(['code' => 403, 'message' => '请选择内容']);
        }
        //是否审批过了
        $is = Approval::where('company_id',$request->company_id)->where('project_id',$request->project_id)->where('model',$request->model)->where('source',$request->source)->where('source_id',$request->source_id)->first();
        if($is){
            if($is->status == 'afoot'){
                return response()->json(['code' => 403, 'message' => '审批进行中，请勿重复提交']);
            }
            if($is->status == 'approved'){
                return response()->json(['code' => 403, 'message' => '已通过，请勿重复提交']);
            }
            if($is->status == 'rejected'){
                return response()->json(['code' => 403, 'message' => '已驳回，请撤销后重新提交']);
            }
        }

        $input['created_by'] = Auth::user()->id;
        $input['company_id'] = $request->company_id;
        $input['project_id'] = $request->project_id;
        $input['model'] = $request->model;
        $input['source'] = $request->source;
        $input['source_id'] = $request->source_id;
        $data = $this->ApprovalService->launch($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    public function review(Request $request)
    {
        $POSTONLY = Approval::POSTONLY;
        $input = $request->only($POSTONLY);
        $admin = Auth::user();
        $input['admin_id'] = $admin->id;
        $CompanyService = new CompanyService();
        $role_id = $CompanyService->getRoleId($request->company_id,$admin->id);
        $input['role_id'] = $role_id;
        $input['approval_id'] = $request->approval_id;
        $input['result'] = $request->result;
        $input['comment'] = $request->comment;
        $input['files'] = $request->files;
        $data = $this->ApprovalService->review($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }
}
